package com.nantian.logic.controller;

import com.cuit.springboot.gdao.user.BusinessMapper;
import com.cuit.springboot.gdao.user.StudentMapper;
import com.cuit.springboot.gentry.user.Business;
import com.nantian.logic.entity.mainEntity.stuEntity.StudentEntity;
import com.nantian.logic.repository.mainRepo.stuRepo.StudentRepository;
import com.nantian.logic.utils.UniversalResult;
import com.nantian.model.security.User;
import com.nantian.security.JwtAuthenticationRequest;
import com.nantian.security.repository.UserRepository;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.Random;

@RestController
@RequestMapping(value = "/general/")
public class NoAuthController {
    private final Log logger = LogFactory.getLog(this.getClass());
    private BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
    @Autowired
    UserRepository userRepository;
    @Autowired
    StudentRepository stuRepo;
    @Autowired
    BusinessMapper busiMapper;
 //   @Autowired
 //   UserMapper userMapper;
    //  获取短信验证码
    @RequestMapping(value = "getCaptcha")
    public UniversalResult getConfirmCode() {
        return new UniversalResult<>(UniversalResult.SUCCESS, "获取验证码成功", "523900");
    }

    @RequestMapping(value = "getLoginCaptcha", method = RequestMethod.POST)
    public UniversalResult getLoginCaptcha(@RequestBody JwtAuthenticationRequest authenticationRequest) {
        String confirmCode = authenticationRequest.getPhoneNum();

        return new UniversalResult<>(UniversalResult.SUCCESS,"获取成功");
    }

    //  验证短信验证码
    @RequestMapping(value = "checkCode", method = RequestMethod.POST)
    public UniversalResult checkCode(@RequestBody Map map) {
        if (map.containsKey("code")&&map.containsKey("phoneNum")){
            String code = map.get("code").toString();
            if ("523900".equals(code))
                return new UniversalResult<>(UniversalResult.SUCCESS, "验证成功");
        }
            return new UniversalResult<>(UniversalResult.FAILURE, "验证失败");
    }
    //  新用户注册
    @RequestMapping(value = "signUp",method = RequestMethod.POST)
    public UniversalResult signUp(@RequestBody Map<String,String> map) {
        User user = new User();
        user.setUsername(map.get("username"));
        user.setPassword(bCryptPasswordEncoder.encode(map.get("password")));
        user.setPhoneNum(map.get("phoneNum"));
        user.setRoletype(map.get("roletype"));
        user.setEnabled(true);
        System.out.println(map.toString());
        boolean ext = userRepository.existsUserByUsername(map.get("username"));
       // User user1 = userRepository.findByUsername(map.get("username"));
        if(!ext){
            Date d = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
            System.out.println("当前时间：" + sdf.format(d));
            Random random = new Random();
            user.setUserid( sdf.format(d).toString()+random.nextInt(10));
            userRepository.saveAndFlush(user);

            //modify 20180302
            if("B".equals(map.get("roletype"))){
                Business business=new Business();
                business.setUserid(user.getUserid());
                busiMapper.insert(business);
            }else if("S".equals(map.get("roletype"))){
                StudentEntity stuEntity=new StudentEntity();
                stuEntity.setPhone(map.get("phoneNum"));
                stuEntity.setUserid(Long.valueOf(user.getUserid()));
                stuEntity.setUsername(map.get("username"));
                stuRepo.save(stuEntity);
            }

            return new UniversalResult<>(UniversalResult.SUCCESS, "注册成功");
        }
        return new UniversalResult<>(UniversalResult.FAILURE, "用户已存在");
    }

}
